package org.zlb.netty.client.udp;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramPacket;

public class ChinesProverClientHandler extends SimpleChannelInboundHandler<DatagramPacket>{

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
        ByteBuf byteBuf = msg.content();
        if (byteBuf.hasArray()) {
            byte[] arr = byteBuf.array();
            int offset = byteBuf.arrayOffset() + byteBuf.readerIndex();
            System.out.println(new String(arr, offset, byteBuf.readableBytes()));
        } else {
            int length = byteBuf.readableBytes();
            byte[] arr = new byte[length];
            byteBuf.getBytes(byteBuf.readerIndex(), arr);
            System.out.println(new String(arr));
        }
    }

}
